package cn.bqjr.eily.utils;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

/**
 * Spring Environment Utils
 *
 * @author hillfly
 */
public class SpringEnvirUtils implements EnvironmentAware {

    private static Environment environment = null;

    @Override
    public void setEnvironment(Environment envir) {
        environment = envir;
    }

    public static Environment getEnvironment() {
        return environment;
    }

    /**
     * 获取配置文件属性值
     * @param name
     * @return
     */
    public static String getProperty(String name) {
        return getEnvironment().getProperty(name);
    }

    /**
     * 获取配置文件属性值
     * @param name
     * @param defaultVal 默认值
     * @return
     */
    public static String getProperty(String name, String defaultVal) {
        return getEnvironment().getProperty(name, defaultVal);
    }

    /**
     * 获取配置文件属性值并转型为对应类型
     * @param name
     * @param cls 转型目标类型
     * @return
     */
    public static <T> T getProperty(String name, Class<T> cls) {
        return getEnvironment().getProperty(name, cls);
    }

    /**
     * 获取配置文件属性值并转型为对应类型
     * @param name
     * @param cls 转型目标类型
     * @param defaultVal 默认值
     * @return
     */
    public static <T> T getProperty(String name, Class<T> cls, T defaultVal) {
        return getEnvironment().getProperty(name, cls, defaultVal);
    }

    /**
     * 获取当前激活的Profiles
     * @return
     */
    public static String[] getProfiles() {
        return getEnvironment().getActiveProfiles();
    }
}
